package com.Ngbussiness.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Ngbussiness.dao.membersDao;
import com.Ngbussiness.dao.yongjinDao;
import com.Ngbussiness.dao.impl.membersDaoImpl;
import com.Ngbussiness.dao.impl.yongjinDaoImpl;
import com.Ngbussiness.util.Calenda;
import com.Ngbussiness.util.DataTools;

public class publishyongjinServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String end_time=request.getParameter("end_time");
		String phone=request.getParameter("phone");
		String sjaddress=request.getParameter("sjaddress");
		int range=Integer.parseInt(request.getParameter("range"));
		double price=Double.parseDouble(request.getParameter("price"));
		int num=Integer.parseInt(request.getParameter("num"));
		Calenda c=new Calenda();
		String start_time=c.getNowTime().toString();
		String img_url=request.getParameter("img_url");
		double probability=Double.parseDouble(request.getParameter("probability"));
		String businessname=request.getParameter("businessname");
		String discription=request.getParameter("discription");
		int category1=Integer.parseInt(request.getParameter("category1"));
		String category2=request.getParameter("category2");
		double longitude=Double.parseDouble(request.getParameter("longitude"));
		double latitude=Double.parseDouble(request.getParameter("latitude"));
		int member_id=Integer.parseInt(request.getParameter("member_id"));
		String bushour=request.getParameter("bushour");
		double zg=price*num;
		System.out.println(zg);
		yongjinDao yj=new yongjinDaoImpl();
		membersDao m=new membersDaoImpl();
		double yongjin=m.getyj(member_id);
		System.out.println(yongjin);
		if(yongjin>=zg){
			boolean b1=yj.insertyj(start_time,end_time,phone,DataTools.HexStringhtoString(sjaddress),range,price,num,img_url,
					probability,DataTools.HexStringhtoString(businessname),DataTools.HexStringhtoString(discription),category1,category2,longitude,latitude,member_id,bushour);
			if(b1==true){
				boolean b=m.getzg(zg,member_id);
				if(b==true){
				out.print("success");
				}
				else{
					out.print("error");
				}
			}
			else{
				out.print("error");
			}
		}
		else{
			out.print("yongjin not enough");
		}
		
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
            doGet(request,response);
	}

}
